<?php

/**
 *
 * 菜单挂件
 * @version $Id:$
 *
 */

namespace Todo\Widget;
use Todo\Service\LoginService;
use Todo\Util\ArrayUtil;
use Think\Controller;

class MenuWidget extends Controller{

	protected function _lists($appId){

	}

	/**
	 * 左侧菜单
	 */
	public function adminlte(){
		//if($appId == 0) $appId = C('APP_ID');
		$appList = D('Todo/Sys/Application')->where(array('status'=>1))->order('sort ASC')->select();
		$user = LoginService::getInstance()->getLoginUser(true);
		if($user['is_super']){
			$menuList = D('Todo/Sys/Menu')->where(array('status'=>1))->order('pid ASC, sort ASC')->select();
		}else{
			$roles = session('user.roles');
			$menus = D('Todo/Sys/RoleMenu')->where(array('role_id' => array('in', $roles)))->select();
			$menuList = D('Todo/Sys/Menu')->where(array('status'=>1, 'id' => array('in', ArrayUtil::getColumn($menus, 'menu_id'))))->order('pid ASC, sort ASC')->select();
		}
		foreach ($appList as &$app){
			if($app['type'] == 3) continue; //扩展和第三方应用，没有菜单
			$appMenus = array();
			foreach ($menuList as $m){
				if($m['app_id'] == $app['id']) {
					$m['target'] = '_blank';
					if($m['type'] == 1) {
						$m['url'] = U($m['url'], array('menuId'=>$m['id']));
						$m['target'] = '_self';
					}
					$appMenus[] = $m;
				}
			}
			if($appMenus) $app['menus'] = ArrayUtil::getTree($appMenus);
		}
		$this->assign('appList', $appList);
		//var_dump($appList);
		$this->display('../data/tpl/adminlte/lte_menu.html');
	}

	/**
	 * 当前位置
	 */
	public function current(){
		$appId = C('APP_ID');
		$appInfo = D('Todo/Sys/Application')->keyFind($appId);
		$this->assign('appInfo', $appInfo);

		$menuId = cookie('current_menu');
		if($menuId){
			$menuInfo = D('Todo/Sys/Menu')->findOne(array('status'=>1,'id'=>$menuId,'app_id'=>$appId));
		}else{
			$url = MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME;
			$menuInfo = D('Todo/Sys/Menu')->findOne(array('status'=>1,'url'=>$url,'app_id'=>$appId));
		}
		$this->assign('menuInfo', $menuInfo);

		$actionInfo = array(
			'edit' => '编辑',
			'add'  => '添加',
			'index' => '列表'
		);
		$this->assign('actionInfo', array('title'=> $actionInfo[strtolower(ACTION_NAME)]));
		$this->display('../data/tpl/adminlte/lte_current.html');
	}
}
?>